<?php

require_once( '../system/smarty.inc' );

require_once( '../system/mdao/Class_M_MATRIX.php' );
require_once( '../system/mdao/Class_M_OTHER.php' );

require_once( '../system/Class_ERROR.php' );
require_once( '../system/login.inc.php' );
require_once( '../system/aoki_m.php' );
require_once( '../system/aoki_m_plus.php' );
require_once( '../system/Class_PWD.php' );

Class_PWD::valid_auth(AOKI_AUTH_MATRIX,$login['staff_auth']);

$isG5  = FALSE;
if   ($login['staff_auth'] == 'G-5') {
	$isG5 = TRUE;
}
$isG7  = FALSE;
if   ($login['staff_auth'] == 'G-7') {
	$isG7 = TRUE;
}

$dosearch = FALSE ;

$dao = new Class_M_MATRIX;


if ( isset( $_GET['csv'] ) &&  (  $_GET['csv'] == "download" ) ||
	isset( $_GET['hist'] ) &&  (  $_GET['hist'] == "2" ) ) {
	$forms =  $_SESSION['MSEARCH']['MATRIX'];
}else {
	$forms = $_GET;
	$_SESSION['MSEARCH']['MATRIX'] = $forms;
}

$wherearr =  array();
if ( isset( $forms['year4'] ) &&  ( !empty( $forms['year4'] ) ) ){
	if ( isset( $forms['months'] ) &&  ( !empty( $forms['months'] ) ) ){
		$month = $forms['months']<10 ? "0".$forms['months'] : $forms['months'];
		$month_begin = $forms['year4'].$month."01";
		$month_end   = $forms['year4'].$month."31";
		$wherearr[] = "( b.flier_date >=  '{$month_begin}' AND b.flier_date <=  '{$month_end}' )";
		
	}else {
		$month_begin = $forms['year4']."0101";
		$month_end   = $forms['year4']."1231";
		$wherearr[] = "( b.flier_date >=  '{$month_begin}' AND b.flier_date <=  '{$month_end}' )";
	}
}

if ( isset( $forms['week_cd'] ) &&  ( !empty( $forms['week_cd'] ) ) ){
	$wherearr[] = " b.matrix_cd =  '{$forms['week_cd']}' ";
}

if ( isset( $forms['upd_f'] ) &&  ( !empty( $forms['upd_f'] ) ) ){
	$wherearr[] = Class_PWD::get_u_time_where_sql($forms['upd_f'], 1, 'b' );
}

if ( isset( $forms['upd_f'] ) &&  ( !empty( $forms['upd_f'] ) ) ){
	$wherearr[] = Class_PWD::get_u_time_where_sql($forms['upd_f'], 1, 'b' );
}


//データ削除
if ( isset( $_GET['csv'] ) &&  (  $_GET['csv'] == "cleartab" ) ){
	
	$dao->droptable( $_GET['matrix_cd']  );
}
//テーブルの作成
if ( isset( $_GET['csv'] ) &&  (  $_GET['csv'] == "cretab" ) ){
	
	$dao->createtable( $_GET['matrix_cd']  );
	
}
//---CSVダウンロード　処理開始
if ( isset( $_GET['csv'] ) &&  (  $_GET['csv'] == "download" ) ){
	$_GET["csv"] = "";
	$tmpfilename = "ins_shop".date("Ymd_His");
	header("Content-Type: application/octet-stream"); 
	header("Content-Disposition: attachment; filename=$tmpfilename.csv");
	$data = $dao->searchcsv( $wherearr );
	$colums = array();
	$col_heads = array(
		'matrix_cd'            => array( 'マトリックスID',  1),
		'year4'                => array( '年度',            1),
		'week_cd'              => array( '週',              1),
		'flier_date'           => array( '折込日',          1),
		'week_date_begin'      => array( '開始日',          1),
		'week_date_end'        => array( '終了日',          1),
		'pre_matrix_cd'        => array( '前年比較週',      1),
		'newyear_flag'         => array( '年末年始フラグ',      1),
		'status'               => array( '状態',            1),
		'draft_create_dt'      => array( 'Draft 販促作成',  1),
		'draft_input_end_dt'   => array( 'Draft 営業入力',  1),
		'draft_modi_end_dt'    => array( 'Draft 販促確認、修正',        1),
		'draft_publish_end_dt' => array( 'Draft 各折込.印刷会社様手配', 1),
		'final_input_end_dt'   => array( 'Final 営業入力',              1),
		'final_modi_end_dt'    => array( 'Final 販促確認、修正',        1),
		'groupid'              => array( 'チラシマトリックスID',        1),
		'title'                => array( 'タイトル',        1),
		'color'                => array( '色',              1),
		'sale_name'            => array( '売り方',          1),
		'oper'                 => array( '継続/新規',          1),
		'verno'                => array( '版番号',          1),
		'size'                 => array( 'サイズ',          1),
		'contractor'           => array( '制作業者',            1),
		'print'                => array( '印刷業者',            1),
		'area'                 => array( 'エリア名',        1),
		'pattern_name'         => array( '新聞紙名',        1),
		
		'mano_out'             => array( '管理NO オモテ',   1),
		'mano_in'              => array( '管理NO ウラ',     1),
		'map'                  => array( 'MAP',             1),
		'saletickt'            => array( '割券',             1),
		
		'perweekver'           => array( '前週下阪',      1),
		'exp_ins_flg'          => array( '例外折込フラグ',  1),
		'exp_ins_week'         => array( '折込日',    1),
		'memo'                 => array( '備考欄',          1),
		'user_flag'            => array( '使用フラグ',      1),
		'u_time'               => array( '更新日時',        3),
		'c_time'               => array( '登録日時',        3),
		'm_user_name'          => array( '更新者',          1)
	);
	//pre_matrix_cd
	
	//列名
	foreach ($col_heads as $k=>$v) {
		$colums["name"][]   = $k;
		$colums["head"][]   = $v[0];
		$colums["type"][$k] = $v[1];
	}
	
	//データ整形開始
	foreach ($data as $k => $v) {
		$data[$k]["exp_ins_flg"]          =  $aoki_set_flags[$v["exp_ins_flg"]];
		
		$data[$k]["flier_date"]           =  Class_PWD::dateTime_to_YmdHi($v["flier_date"]);
		$data[$k]["status"]               =  $aoki_matrix_status[Class_PWD::getMatrixStatues($v)];
		$data[$k]["draft_create_dt"]      =  Class_PWD::dateTime_to_YmdHi($v["draft_create_dt"]);
		$data[$k]["draft_input_end_dt"]   =  Class_PWD::dateTime_to_YmdHi($v["draft_input_end_dt"]);
		$data[$k]["draft_modi_end_dt"]    =  Class_PWD::dateTime_to_YmdHi($v["draft_modi_end_dt"]);
		$data[$k]["draft_publish_end_dt"] =  Class_PWD::dateTime_to_YmdHi($v["draft_publish_end_dt"]);
		$data[$k]["final_input_end_dt"]   =  Class_PWD::dateTime_to_YmdHi($v["final_input_end_dt"]);
		$data[$k]["final_modi_end_dt"]    =  Class_PWD::dateTime_to_YmdHi($v["final_modi_end_dt"]);
		$data[$k]["c_time"]               =  Class_PWD::dateTime_to_YmdHi($v["c_time"]);
		$data[$k]["u_time"]               =  Class_PWD::dateTime_to_YmdHi($v["u_time"]);
		
		$data[$k]["week_cd"]       = $v["week_seq"]."週";
		if ( $v["week_seq"]) {
			$data[$k]["week_cd"]   = $data[$k]["week_cd"]."第".$v["week_seq"]."回";
		}
		if ($v["pre_matrix_cd"]) {
			$prow   = Class_M_OTHER::get_by_matrix_cd($v["pre_matrix_cd"]);
			$data[$k]["pre_matrix_cd"] = empty($prow["week_seq"]) ? $prow["week_seq"]."週" : $prow["week_seq"]."週第".$prow["week_seq"]."回";
			
		}else {
			$data[$k]["pre_matrix_cd"] = "";
		}
	}
	//データ整形完了
	
	$dao->output_csv($data, $colums);
	exit;
}

//---CSVダウンロード　処理完了
if ( isset( $forms['search'] ) && (  !empty( $forms['search'] ) ) ){
	$dosearch = TRUE;
}

if( isset( $forms['page'] ) && is_numeric( $forms['page'] ) ){
	$dosearch = TRUE;
	$page['current'] = $forms['page'];	
}else{
	$page['current'] = 1;
}

$page['item'] = PAGE_MAX;
if ($dosearch) {
	//$page内容は関数内部更新されます。
	$data = $dao->search( $wherearr, $page );
	//データ整形
	foreach ($data as $k=>$v) {
		$data[$k]["week_data_flg"]       =  empty($v["week_data_flg"]) ? '0' :   $v["week_data_flg"];
		$data[$k]["week_data_flg_name"]   =  $aoki_matrix_data_flgs[$v["week_data_flg"]];
		
		$data[$k]["flier_date"] = Class_PWD::int8_to_dateD($v["flier_date"]);
		$tempT = "00";
		$tmp_D    = Class_PWD::dateTime_to_dateD($v["draft_modi_end_dt"], &$tempT);
		$data[$k]["draft_modi_end_dt"] = $tmp_D."&nbsp; ".$tempT."時";
		$tempT = "00";
		$tmp_D    = Class_PWD::dateTime_to_dateD($v["final_modi_end_dt"], &$tempT);
		$data[$k]["final_modi_end_dt"] = $tmp_D."&nbsp; ".$tempT."時";
		$data[$k]["ver"]     = $dao->getVerCount($v["matrix_cd"] );
		$data[$k]["status"]  = $aoki_matrix_status[Class_PWD::getMatrixStatues($v)];
		$week = $v["year4"]."年".$v["week_cd"]."週";
		if ( $v["week_seq"]) {
			$week =$week."第".$v["week_seq"]."回";
		}
		$data[$k]["week"]    =$week;
	}
}

if (empty($forms["year4"])) {
	$forms["year4"] = date('Y');
}

if( isset($forms) ) $smarty->assign('forms', $forms);
if( isset($data) )  $smarty->assign('data', $data);
if( isset($page) )  $smarty->assign('page', $page);


$smarty->assign('isG5', $isG5 );
$smarty->assign('isG7', $isG7 );

$smarty->assign('year4',  Class_PWD::get_years());

$week = Class_M_OTHER::get_week_array( $forms["year4"], PLEASE_SELECT_ARR, TRUE );
$smarty->assign('week', $week );

$months = array();
$months[""] = PLEASE_SELECT_ARR;
for( $a = 1; $a <= 12; $a++ )
{
	$months[$a] = "&nbsp;".$a;
}
$smarty->assign('months', $months );

$smarty->assign('menu',  $smarty->fetch('menu.html') );
$smarty->assign('logout', $smarty->fetch('logout.html') );
$smarty->assign('footer', $smarty->fetch('footer.html') );
$smarty->assign('pageing', $smarty->fetch('page.html') );
$smarty->display('mmatrix_index.html');
//データ整形

function _format($data) {
	foreach ($data as $k=>$v) {
		$data[$k]["flier_date"] = Class_PWD::int8_to_dateD($v["flier_date"]);
	}
}
?>